﻿package com.badibadi.activity;

import java.util.ArrayList;
import java.util.List;

import com.badibadi.activity.BaseActivity.ViewFragmentHolder;
import com.badibadi.fragment.ActivityOverViewFragment0;
import com.badibadi.fragment.CalendarFragment;
import com.badibadi.fragment.CalendarFragment.CalendarGridViewAdapter;
import com.badibadi.fragment.CalendarFragment.CalendarListener;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.mytools.TabHostUtils;
import com.badibadi.mytools.Constants;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CalendarView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 日历的activity，所有活动继承自他（目前是期间内的活动）
 * 
 * @author Administrator Intent
 * 
 */
public class CalendarActivity extends BaseActivity implements OnClickListener {
	public RelativeLayout bottom_btn1, bottom_btn2;
	// /...黄色是当天，淡蓝色是点击瞬间，深蓝是有活动的天，开始一个标签，结束一个标签
	public TabHost tabhost;
	public TabHost tabhost2;
	public CalendarFragment calendarFragment;
	// private CalendarListFragment calendarListFragment;
	protected ActivityOverViewFragment0 fragment0;

	public FragmentManager manager;
	private Bundle bun;
	private int StartYY, StartMM, StartDD;
	private int EndYY, EndMM, EndDD;
	private int Judge_YY, Judge_MM, Judge_DD;
	private String d_content;
	// 保存从查询出传过来的东西
	private ClueTypeModel clueTypeModel;
	protected List<Activity_Overview_Model> models;
	public LinearLayout Mode_switching;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.calendar);
		initGuanJianZi();
		init();
		LoadFragment(0, models);
		// initCalendarFragment();
	}

	/**
	 * 这个是用于接收从查询处传过来的东西
	 */
	private void initGuanJianZi() {
		// TODO Auto-generated method stub
		clueTypeModel = (ClueTypeModel) getIntent().getSerializableExtra(
				"CalendarActivity");
		if (clueTypeModel != null) {
			if (!StringUtil.isNullOrEmpty(clueTypeModel.getName()))
				((TextView) (findViewById(R.id.calendar_title))).setText(""
						+ clueTypeModel.getName());
		}
	}

	/**
	 * 要重写的第一处（初始化为tabhost2）
	 * 
	 * @param id
	 * @param id2
	 */
	protected void initTabHostTWO(int id, int id2) {
		// TODO Auto-generated method stub
		manager = getSupportFragmentManager();
		tabhost2.setVisibility(View.GONE);
		tabhost.setVisibility(View.VISIBLE);
		tabhost.setup();
		tabhost.addTab(tabhost
				.newTabSpec("calendar_tabhost one")
				.setIndicator(getResources().getString(id),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		tabhost.addTab(tabhost
				.newTabSpec("calendar_tabhost two")
				.setIndicator(getResources().getString(id2),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		TabHostUtils.SetTabHost(this, tabhost);
		tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			@Override
			public void onTabChanged(String arg0) {
				// TODO Auto-generated method stub
				switch (tabhost.getCurrentTab()) {
				case 0:
					break;
				case 1:
					break;
				}
			}
		});

	}

	/**
	 * 要重写的第二处重写tabhost
	 * 
	 * @param id
	 * @param id2
	 * @param id3
	 */
	public void initTabHostTHREE(int id, int id2, int id3) {
		tabhost.setVisibility(View.GONE);
		tabhost2.setVisibility(View.VISIBLE);
		tabhost2.setup();
		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost one")
				.setIndicator(getResources().getString(id),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost two")
				.setIndicator(getResources().getString(id2),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));

		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost three")
				.setIndicator(getResources().getString(id3),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost four")
				.setIndicator(getResources().getString(R.string.Deaeline),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab4));
		TabHostUtils.SetTabHost(this, tabhost2);
		tabhost2.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String arg0) {
				// TODO Auto-generated method stub

				switch (tabhost2.getCurrentTab()) {
				case 0:
					initListFragment(1, "");
					break;
				case 1:
					initListFragment(2, "");
					break;
				case 2:
					initListFragment(3, "");
					break;
				case 3:
					initListFragment(4, "");
					break;
				}
			}
		});

	}

	protected boolean ShowStartButton(int Y, int M, int D, int YY, int MM,
			int DD) {
		if (YY == Y && MM == M && DD == D) {
			return true;
		} else {
			return false;
		}
	}

	protected boolean ShowEndButton(int Y, int M, int D, int YY, int MM, int DD) {
		if (YY == Y && MM == M && DD == D) {
			return true;
		} else {
			return false;
		}
	}

	public String ShowActivity(int Y, int M, int D,
			List<Activity_Overview_Model> models) {
		if (models != null && !models.isEmpty()) {
			for (int i = 0; i < models.size(); i++) {
				if (Y == BackYear(models.get(i).getStart_time())
						&& M == BackMonth(models.get(i).getStart_time())
						&& D == BackDay(models.get(i).getStart_time())) {
					return getResources().getString(R.string.l_xb40) + panduan(Y, M, D, models) + getResources().getString(R.string.l_xb41);
				}
			}
		}
		return null;
	}

	/**
	 * 判断共有多少个活动
	 * 
	 * @param Y
	 * @param M
	 * @param D
	 * @param models
	 * @return
	 */
	private int panduan(int Y, int M, int D,
			List<Activity_Overview_Model> models) {
		int num = 0;
		Constants.temp_num = new ArrayList<Integer>();
		for (int i = 0; i < models.size(); i++) {
			if (Y == BackYear(models.get(i).getStart_time())
					&& M == BackMonth(models.get(i).getStart_time())
					&& D == BackDay(models.get(i).getStart_time())) {
				Constants.temp_num.add(i);
				++num;
			}
		}
		Constants.temp_map.put(Y + "-" + M + "-" + D, Constants.temp_num);
		// Constants.temp_list.add(Constants.temp_num);
		return num;
	}

	// 将时间中的年份提取出来并转换为int
	public static int BackYear(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(0, 4));
		} else {
			return 0;
		}
	}

	// 将时间中的月份提取出来并转换为int
	public static int BackMonth(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(5, 7));
		} else {
			return 0;
		}
	}

	// 将时间中的日提取出来并转换为int
	public static int BackDay(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(8, time.length()));
		} else {
			return 0;
		}
	}

	/**
	 * 要在日历中加入数据在这里改
	 */
	protected void initCalendarFragment(
			final List<Activity_Overview_Model> models) {
		// TODO Auto-generated method stub
		FragmentTransaction transaction = manager.beginTransaction();
		calendarFragment = CalendarFragment.getInstance();
		calendarFragment
				.setOnCalendarOnClistener(new CalendarFragment.CalendarListener() {

					@Override
					public boolean AddStartButton(int Year, int Month, int Day) {
						// TODO Auto-generated method stub
                            
						return ShowStartButton(Year, Month, Day, StartYY,
								StartMM, StartDD);

					}

					@Override
					public boolean AddEndButton(int Year, int Month, int Day) {
						// TODO Auto-generated method stub
						return ShowEndButton(Year, Month, Day, EndYY, EndMM,
								EndDD);
					}

					@Override
					public String AddActivity(int Year, int Month, int Day) {
						// TODO Auto-generated method stub

						// if (Year == 2014 && Month == 8 && Day == 8) {
						// return "是福建省懂了否";
						// }
						// if (Year == 2014 && Month == 8 && Day == 9) {
						// return "是福建省懂了";
						// }

						return ShowActivity(Year, Month, Day, models);
					}
				});
		calendarFragment
				.setOnCalendarItemClistener(new CalendarFragment.CalendarGridViewListener() {

					@Override
					public void ItemListener(AdapterView<?> arg0, View view,
							int position, long arg3,
							CalendarGridViewAdapter calendaradapter) {
						// TODO Auto-generated method stub
						initOnCalendarItemClistener(arg0, view, position, arg3,
								calendaradapter);
					}
				});
		transaction.replace(R.id.calendar_layout, calendarFragment);
		transaction.commit();
	}

	/**
	 * 用子类去继承他重写点击事件
	 * 
	 * @param arg0
	 * @param view
	 * @param position
	 * @param arg3
	 * @param calendaradapter
	 */
	protected void initOnCalendarItemClistener(AdapterView<?> arg0, View view,
			int position, long arg3, CalendarGridViewAdapter calendaradapter) {
		// Toast.makeText(getApplicationContext(), "position" + position, 0)
		// .show();
	}

	/**
	 * 根据num判断读取的是哪一个Fragment
	 * 
	 * @param num
	 */
	protected void LoadFragment(int num, List<Activity_Overview_Model> models) {
		if (num == 0) {
			initCalendarFragment(models);
		} else if (num == 1) {
			initListFragment(1, "");
		}
	}

	/**
	 * 列表的Fragment
	 */
	public void initListFragment(int t, String yes) {
		// TODO Auto-generated method stub
		FragmentTransaction transaction = manager.beginTransaction();
		fragment0 = new ActivityOverViewFragment0();
		bun = new Bundle();
		bun.putInt("type", t);
		if (yes.equals("yes")) {
			bun.putString("yincang", "yes");
		}
		fragment0.setArguments(bun);
		transaction.replace(R.id.calendar_layout, fragment0);
		transaction.commit();
	}

	/**
	 * 在这里是否要隐藏搜索关键字的和类别的
	 * 
	 * @param ll
	 * @param visible
	 *            0 Visible -1 gone 1 VISIBLE
	 */
	protected void GoneOrVisibleSearchEdiTText(LinearLayout ll, int visible) {
		// TODO Auto-generated method stub
		if (visible == 1) {
			ll.setVisibility(View.VISIBLE);
		} else if (visible == -1)
			ll.setVisibility(View.GONE);
		else if (visible == 0) {
			ll.setVisibility(View.INVISIBLE);
		}

	}

	private void init() {
		// TODO Auto-generated method stub
		tabhost = (TabHost) findViewById(R.id.calendar_tabhost);
		tabhost2 = (TabHost) findViewById(R.id.RigionPicture_tabhost);
		bottom_btn1 = (RelativeLayout) findViewById(R.id.calender_bottom_btn1);
		bottom_btn2 = (RelativeLayout) findViewById(R.id.calender_bottom_btn2);
		Mode_switching = (LinearLayout) findViewById(R.id.Mode_switching);
		bottom_btn1.setSelected(true);
		bottom_btn2.setSelected(false);
		models = new ArrayList<Activity_Overview_Model>();

		this.findViewById(R.id.calendar_return_btn).setOnClickListener(this);
		this.findViewById(R.id.calendar_index_btn).setOnClickListener(this);
		// RelativeLayout
		// rl=(RelativeLayout)this.findViewById(R.id.calender_bottom_btn2);
		initTabHostTWO(R.string.Sponsor, R.string.Partake);
		initTabHostTHREE(R.string.Newest, R.string.Hottest, R.string.Create);
		ChangeToTWOTab();

	}

	/**
	 * 底部日历模式的点击事件
	 * 
	 * @param view
	 */
	public void OnClick0(View view) {
		if (view.isSelected() != true) {
			view.setSelected(true);
			bottom_btn2.setSelected(false);
			LoadFragment(0, models);
			OnChageTabHost0();

		}
	}

	/**
	 * 底部列表的点击事件
	 * 
	 * @param view
	 */
	public void OnClick1(View view) {
		if (view.isSelected() != true) {
			view.setSelected(true);
			bottom_btn1.setSelected(false);

			LoadFragment(1, models);
			OnChageTabHost1();
		}
	}

	/**
	 * 必须重写
	 */
	public void OnChageTabHost0() {
		// TODO Auto-generated method stub
		ChangeToTWOTab();
	}

	/**
	 * 必须重写
	 */
	public void OnChageTabHost1() {
		// TODO Auto-generated method stub
		ChangeToThreeTab();
	}

	protected void ChangeToThreeTab() {
		// TODO Auto-generated method stub

		tabhost.setVisibility(View.GONE);
		tabhost2.setVisibility(View.VISIBLE);
	}

	protected void ChangeToTWOTab() {

		// initTabHost(id,id2);
		tabhost2.setVisibility(View.GONE);
		tabhost.setVisibility(View.VISIBLE);

	}

	protected void GoneTab() {
		tabhost2.setVisibility(View.GONE);
		tabhost.setVisibility(View.GONE);
	}

	/**
	 * 隐藏掉所有的tabHost
	 */
	protected void GoneAllTabHost() {
		tabhost2.setVisibility(View.GONE);
		tabhost.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.calendar_return_btn:
			CalendarActivity.this.finish();
			break;
		case R.id.calendar_index_btn:
			Intent intent = new Intent(CalendarActivity.this,
					IndexActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

}
